#!/bin/bash

# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# 获取项目根目录(脚本所在目录的父目录)
PROJECT_ROOT="$( cd "$SCRIPT_DIR/.." && pwd )"

# 入口文件路径
ENTRY_FILE="$PROJECT_ROOT/cmd/api/main.go"

# 打包函数 - 复制资源到目标目录
package_resources() {
  local target_dir=$1
  local app_name=${2:-"myapp"}
  local resources=("$PROJECT_ROOT/static" "$PROJECT_ROOT/templates" "$PROJECT_ROOT/config")
  
  # 创建目标目录
  mkdir -p "$target_dir"
  
  # 根据当前系统确定可执行文件扩展名
  local bin_ext=""
  if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
    bin_ext=".exe"
  fi
  
  # 编译应用 - 使用绝对路径
  echo "Building application for current platform..."
  go build -o "${target_dir}/${app_name}${bin_ext}" "$ENTRY_FILE"
  
  # 复制资源文件 - 使用绝对路径
  for dir in "${resources[@]}"; do
    if [ -d "$dir" ]; then
      echo "Copying $dir to ${target_dir}/$(basename "$dir")"
      cp -r "$dir" "${target_dir}/"
    else
      echo "Warning: Resource directory $dir not found"
    fi
  done
}

# 创建压缩包
create_archive() {
  local source_dir=$1
  local output_file=$2
  local archive_type=$3
  
  echo "Creating archive: $output_file"
  
  if [ "$archive_type" == "zip" ]; then
    (cd $(dirname "$source_dir") && zip -r "$output_file" $(basename "$source_dir"))
  elif [ "$archive_type" == "tar" ]; then
    (cd $(dirname "$source_dir") && tar -czf "$output_file" $(basename "$source_dir"))
  else
    echo "Unknown archive type: $archive_type"
    return 1
  fi
  
  echo "Archive created: $output_file"
}

# 打包命令处理
if [ "$1" == "build" ]; then
  if [ "$2" == "win" ]; then
    echo "Building for Windows..."
    mkdir -p ../bin/windows
    GOOS=windows GOARCH=amd64 go build -o ../bin/windows/myapp.exe $ENTRY_FILE
    for dir in ../static ../templates ../config; do
      if [ -d "$dir" ]; then
        cp -r "$dir" "../bin/windows/$(basename "$dir")"
      fi
    done
    exit 0
    
  elif [ "$2" == "linux" ]; then
    echo "Building for Linux..."
    mkdir -p ../bin/linux
    GOOS=linux GOARCH=amd64 go build -o ../bin/linux/myapp $ENTRY_FILE
    for dir in ../static ../templates ../config; do
      if [ -d "$dir" ]; then
        cp -r "$dir" "../bin/linux/$(basename "$dir")"
      fi
    done
    exit 0
    
  elif [ "$2" == "package-win" ]; then
    echo "Packaging for Windows..."
    mkdir -p ../bin/windows
    GOOS=windows GOARCH=amd64 go build -o ../bin/windows/myapp.exe $ENTRY_FILE
    for dir in ../static ../templates ../config; do
      if [ -d "$dir" ]; then
        cp -r "$dir" "../bin/windows/$(basename "$dir")"
      fi
    done
    (cd ../bin && zip -r myapp_windows.zip windows)
    exit 0
    
  elif [ "$2" == "package-linux" ]; then
    echo "Packaging for Linux..."
    mkdir -p ../bin/linux
    GOOS=linux GOARCH=amd64 go build -o ../bin/linux/myapp $ENTRY_FILE
    for dir in ../static ../templates ../config; do
      if [ -d "$dir" ]; then
        cp -r "$dir" "../bin/linux/$(basename "$dir")"
      fi
    done
    (cd ../bin && tar -czf myapp_linux.tar.gz linux)
    exit 0
    
  elif [ "$2" == "package" ]; then
    echo "Packaging for all platforms..."
    # Windows
    mkdir -p ../bin/windows
    GOOS=windows GOARCH=amd64 go build -o ../bin/windows/myapp.exe $ENTRY_FILE
    for dir in ../static ../templates ../config; do
      if [ -d "$dir" ]; then
        cp -r "$dir" "../bin/windows/$(basename "$dir")"
      fi
    done
    
    # Linux
    mkdir -p ../bin/linux
    GOOS=linux GOARCH=amd64 go build -o ../bin/linux/myapp $ENTRY_FILE
    for dir in ../static ../templates ../config; do
      if [ -d "$dir" ]; then
        cp -r "$dir" "../bin/linux/$(basename "$dir")"
      fi
    done
    
    # Create archives
    (cd ../bin && zip -r myapp_windows.zip windows)
    (cd ../bin && tar -czf myapp_linux.tar.gz linux)
    exit 0
    
  elif [ "$2" == "custom" ]; then
    if [ -z "$3" ]; then
      echo "Error: Target directory not specified"
      echo "Usage: ./scripts/deploy.sh build custom <target_dir>"
      exit 1
    fi
    
    echo "Building to custom directory: $3"
    package_resources "$3"
    exit 0
  else
    echo "Unknown build command: $2"
    echo "Available commands:"
    echo "  ./scripts/deploy.sh build win             - Build for Windows"
    echo "  ./scripts/deploy.sh build linux           - Build for Linux"
    echo "  ./scripts/deploy.sh build package-win     - Build and package for Windows"
    echo "  ./scripts/deploy.sh build package-linux   - Build and package for Linux"
    echo "  ./scripts/deploy.sh build package         - Build and package for all platforms"
    echo "  ./scripts/deploy.sh build custom <dir>    - Build for current platform to custom directory"
    exit 1
  fi

elif [ "$1" == "docs" ]; then
  echo "Generating API documentation..."
  cd "$PROJECT_ROOT"
  swag init -g "./cmd/api/main.go" -o docs
  exit 0

fi 